package com.javaspeak.algorithm.fibonacci.notrecursive;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * @author John Dickerson
 */
public class FibonacciGeneratorNonRecursive {

    private static Map<Integer,BigInteger> fibonacciSequence =
        new HashMap<Integer,BigInteger>();

    static {
        fibonacciSequence.put( 1, BigInteger.ONE );
        fibonacciSequence.put( 2, BigInteger.ONE );
    }


    public static BigInteger getFibonnacci( int i ) {

        for ( int y=fibonacciSequence.size()+1; y<=i; y++ ){

            BigInteger previous = fibonacciSequence.get( y - 1);
            BigInteger beforePrevious = fibonacciSequence.get( y - 2 );
            fibonacciSequence.put( y, previous.add( beforePrevious ) );
        }

        return fibonacciSequence.get( i );
    }
}
